package com.dongdongshop.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.exception.SellerLoginException;
import com.dongdongshop.pojo.TbSeller;
import com.dongdongshop.pojo.User;
import com.dongdongshop.service.SellerService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.Map;

@Controller
@RequestMapping("shopLogin")
public class ShopLoginController {

    @Reference
    private SellerService sellerService;

    @RequestMapping("login")
    public String login(){
        return "shoplogin";
    }

    @RequestMapping("tologin")
    @ResponseBody
    public Map<String, Object> login(HttpServletRequest request, TbSeller seller){
        Map<String,Object> map = new LinkedHashMap<>();
        map.put("flag",false);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(seller.getSellerId(),seller.getPassword());
        try{
            subject.login(token);
        }catch (UnknownAccountException e){
            map.put("msg","账户不存在");
            return map;
        }catch (IncorrectCredentialsException e1){
            map.put("msg","密码错误");
            return map;
        }catch (SellerLoginException e){
            map.put("msg","未审核");
            return map;
        }
        map.put("flag",true);
        sellerService.getName(seller.getSellerId());
        return map;
    }

    @RequestMapping("index")
    public String index(){
        return "admin/index";
    }

    @RequestMapping("error")
    public String error(){
        return "error";
    }
}
